package com.simple.car.loader.server.entity;

import com.simple.car.loader.server.annotation.RequestParam;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Executor {

    private Method method;

    private Object object;

    private List<String> pName;

    private List<Class<?>> classes;

    public static Executor fromMethod(Object instance, Method method) {
        Executor executor = new Executor();
        executor.object = instance;
        executor.method = method;
        Parameter[] parameters = method.getParameters();
        executor.pName = new ArrayList<>();
        executor.classes = new ArrayList<>();
        if (parameters.length > 0) {
            for (Parameter parameter : parameters) {
                if (parameter.getType().equals(Socket.class)){
                    executor.pName.add(parameter.getName());
                    executor.classes.add(parameter.getType());
                    continue;
                }
                RequestParam requestParam = parameter.getDeclaredAnnotation(RequestParam.class);
                if (requestParam == null) {
                    throw new IllegalArgumentException(String.format("缺少注解标记：%s", RequestParam.class.getName()));
                }
                executor.pName.add(requestParam.value());
                executor.classes.add(parameter.getType());
            }
        }
        return executor;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public List<String> getpName() {
        return pName;
    }

    public void setpName(List<String> pName) {
        this.pName = pName;
    }

    public List<Class<?>> getClasses() {
        return classes;
    }

    public void setClasses(List<Class<?>> classes) {
        this.classes = classes;
    }
}
